<?php

namespace app\admin\controller;

use think\Exception;
use think\exception\ValidateException;
use think\facade\Filesystem;
use think\facade\Request;
use think\facade\Validate;

class Upload
{
    public static function imgUpload($filename = 'img', $dir = 'linshi/upload', $rule = 'data')
    {
        try {
            $file = Request::file();
            validate([$filename => 'filesize:102400|fileExt:jpg,png'])->check($file);
            $paths = [];
            if (is_array($file[$filename])) {
                foreach ($file[$filename] as $img) {
                    $path = Filesystem::disk('public')->putFile($dir, $img, $rule);
                    $paths[] = str_replace('\\', '/', $path);
                }
                $paths = implode(",", $paths);
            } else {
                $path = Filesystem::disk('public')->putFile($dir, $file[$filename], $rule);
                $paths = str_replace('\\', '/', $path);
            }
        } catch (ValidateException $e) {
            return [false, $e->getMessage()];
        } catch (Exception $e) {
            $paths = '';
        }
        return [true, $paths];


    }


    public function ajaxUpload()
    {

        list($status, $dir) = self::imgUpload();

        return json($dir);
    }
}